//app.js
var http = require('/utils/http.js')

App({
  http,
  onLaunch: function () {
    var that = this;
    /**
     * 获取当前设备的信息
     */
    wx.getSystemInfo({
      success: function(res) {
        that.globalData.winWidth = res.windowWidth
        that.globalData.winHeight = res.windowHeight
        that.globalData.barHeight = res.statusBarHeight
        that.globalData.platform = res.platform //设备类型 android ios pc
        if(res.platform == "ios"){
          that.getIosPayStatus()
        }
      }
    });
    var wxUserId = wx.getStorageSync('wxUserId')
    this.globalData.wxUserId = wxUserId
    // 展示本地存储能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)

    // if(wxUserId != null && wxUserId != ''){
    //   this.http.get('/wechat/getUserType?wxUserId=' + wxUserId).then(res =>{
    //     if(res.status == '200'){
    //       console.log('--------wx user type:'+res.data +'----------')
    //       this.globalData.wxUserType = res.data
    //     }
    //   })
    // }

    // 登录
    this.wxLogin()
    // 获取用户信息
    wx.getSetting({
      success: res => {
        console.log("wechat getSetting scope.userInfo:" + res.authSetting['scope.userInfo'])
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              console.log("wechat getUserInfo res:" + res.userInfo.nickName)
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  wxLogin: function() {
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var data = {
          code: res.code
        };
        http.get('/wechat/getWechatOpenid?code=' + res.code).then(res =>{
          if(res.status == '200') {
            console.log("wx code get openid succ!!!")
            // session_key + openid
            this.globalData.session = res.data
          }
        })
      }
    })
  },
  getIosPayStatus: function() {
    http.get('/wechat/getIosPay').then(res => {
      if(res.status == '200'){
        this.globalData.iosPayStatus = true
      } else {
        this.globalData.iosPayStatus = false
      }
    })
  },
  globalData: {
    barHeight: 0, //头部导航栏高度
    winWidth: 0, //设备宽度
    winHeight: 0, //设备高度
    platform: '', //设备类型 android ios pc
    iosPayStatus: false, //ios 支付组件状态
    session: null,
    wxUserId: '',
    shareId: '', //分享人
    wxUserType: '',
    userInfo: null
  }
})